package com.logic.landseaserver.models.changeroom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.Project;
import com.logic.landseaserver.models.changeroom.bean.ChangeRoomContract;
import com.logic.landseaserver.models.changeroom.bean.ChangeRoomOffrentConfirm;
import com.logic.landseaserver.models.changeroom.bean.ChangeRoomReq;
import com.logic.landseaserver.models.changeroom.service.ChangeRoomService;
import com.logic.landseaserver.service.IContractService;
import com.logic.landseaserver.ws.dto.OffrentConfirmDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：ChangeRoomRest</br>
 * 类描述：换房</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年12月13日 上午10:36:06</br>
 * 
 * @version 1.0
 *
 */
@Path("/changeroom")
@Api(value = "换房")
public class ChangeRoomRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ChangeRoomRest.class);
    
    @Autowired
    private ChangeRoomService changeRoomService;
    
    @Autowired
    private IContractService contractService;
    
    /**
     * 
     * [简要描述]：新增换房记录</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-12)</br>
     *
     * @param request
     * @return
     *
     */
    @POST
    @Path("/createChangeRoom")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增换房记录")
    public Response createChangeRoom(@ApiParam(value = "param", required = true) ChangeRoomReq request)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            Map<String, Object> resultMap = changeRoomService.createChangeRoom(request);
            
            Integer destContractId = (Integer)resultMap.get("destContractId");
            if (null != destContractId)
            {
                // 生成PDF
                contractService.createPdf(destContractId);
                LOGGER.info("创建换房合同->生成合同PDF完毕.");
            }
            
            map.setData(resultMap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "createChangeRoom", e, map);
        }
        catch (LandseaBusinessException e)
        {
            LOGGER.error("createChangeRoom error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        catch (Exception e)
        {
            LOGGER.error("createChangeRoom create pdf error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：查询换房记录</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-12)</br>
     *
     * @param request
     * @return
     *
     */
    @GET
    @Path("/queryChangeRoom/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询换房记录")
    public Response queryChangeRoom(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            ChangeRoomContract changeRoomContract = changeRoomService.queryChangeRoom(contractId);
            map.setData(changeRoomContract);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "queryChangeRoom", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @DELETE
    @Path("/cancelChangeRoom/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "取消换房 contractId->A合同编号")
    public Response cancelChangeRoom(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            HashMap<String, Object> result = new HashMap<>();
            changeRoomService.cancelChangeRoom(contractId);
            result.put("result", true);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "cancelChangeRoom", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/createChangeRoomPreBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成换房预结算单")
    public Response createChangeRoomPreBill(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            changeRoomService.createChangeRoomPreBill(contractId);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "cancelChangeRoom", e, map);
        }
        catch (Exception e)
        {
            LOGGER.error("createChangeRoomPreBill error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/changeRoomGetProject/{projectId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询同属子公司的其他项目")
    public Response changeRoomGetProject(
        @ApiParam(value = "param", required = true) @PathParam(value = "projectId") Integer projectId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            List<Project> projectList = changeRoomService.changeRoomGetProject(projectId);
            map.setData(projectList);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "changeRoomGetProject", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/synOverList/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "结转单处理 contractId->B合同编号")
    public Response synOverList(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            changeRoomService.synOverList(contractId);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "synOverList", e, map);
        }
        catch (Exception e)
        {
            LOGGER.error("synOverListf error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/confirmChangeRoom")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "确认换房->提交换房确认")
    public Response confirmChangeRoom(
        @ApiParam(value = "param", required = true) ChangeRoomOffrentConfirm offrentConfirm)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            changeRoomService.confirmChangeRoom(offrentConfirm);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "confirmChangeRoom", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getConfirmChangeRoom/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询换房确认")
    public Response getConfirmChangeRoom(
        @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            OffrentConfirmDTO offrentConfirmDTO = changeRoomService.getConfirmChangeRoom(contractId);
            map.setData(offrentConfirmDTO);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (BusinessException e)
        {
            LOGGER.error("getConfirmChangeRoom error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getConfirmChangeRoom", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    
}
